﻿package Game.LogicSystem{
	import Game.DisplaySystem.DisplayManager;
	import Game.LogicSystem.Objects.*;
	import Game.Manager;
	import Game.MapSystem.MapManager;
	import Game.Utils.FileLoader;
	import Game.Utils.T;
	
	import flash.utils.getDefinitionByName;
	
	
	
	public class ObjectManager extends Manager{
		private var a_ObjectList:Array = new Array();
		
		private var possibleObjectsArray:Array = new Array();
		
		private var m_DisplayManager:DisplayManager;
		private var m_ActionManager:ActionManager;
		
		public function ObjectManager(_parentObject:*,_displayOne:*,_actionOne:*){
			super(_parentObject);
			m_DisplayManager= _displayOne;
			m_ActionManager= _actionOne;
		}
		
		
		//Fonction de Chargement des Objets Initiaux
		public function showObjectMap(_xmladress:String):void {
			FileLoader.LoadObject(_xmladress,onObjectsLoaded,'xml');
		}
	
		public function onObjectsLoaded(_datas:XML):void {
			//TraceXmlMap(_dataArray);
			var j:int;
			var i:int=0;			
			for each(var XmlObject:XML in _datas.children()){
				var posArray:Array = MapManager.getPositionForAdress(XmlObject.attribute('x'),XmlObject.attribute('y'));
				var objType:String = XmlObject.attribute('type');
				
				T.Trace("Game.LogicSystem.Objects."+XmlObject.name(),2,"ObjectManager/showObjectMapListener");
				var dynamicClass:Class = getDefinitionByName("Game.LogicSystem.Objects."+XmlObject.name()) as Class;
				a_ObjectList[i]=new dynamicClass(posArray[0],posArray[1],objType,XmlObject,this);
				if(!isAlreadyRegistered(a_ObjectList[i])){
					possibleObjectsArray.push(a_ObjectList[i]);
				}
				dynamicClass=null;
				i++;
			}
			displayObjects();
		}
		public function isAlreadyRegistered(_obj:ObjectClass):Boolean{
			var result:Boolean = false;
			for(var i:int=0;i<possibleObjectsArray.length;i++){
				if(_obj== possibleObjectsArray[i]as ObjectClass){
					result=true;
				}
			}
			return result;
		}
		
		
		public function displayObjects():void{
			for (var i:int=0; i < a_ObjectList.length;i++){
				m_DisplayManager.showObject(a_ObjectList[i]);
			}
		}
		
		public function getActionsFor(_object:ObjectClass):void{
			m_ActionManager.askForActions(_object);
		}
		public function getObjectListArray():Array{
			return possibleObjectsArray;	
		}
		public function getMoneyValue():int{
			return 1000;
		} 
	}
}